package com.dcbeida.activity.clerkmanager;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.bean.Customer;
import com.dcbeida.listern.DialogListener;
import com.dcbeida.utils.DialogUtils;
import com.dcbeida.utils.Utils;

import butterknife.Bind;

/**
 * 店员状态
 */
public class ClerkStatueActivity extends BaseActivity implements View.OnClickListener,
        ClerkStatusContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRright;
    @Bind(R.id.tv_disable)
    TextView tvDisable;
    @Bind(R.id.tv_status)
    TextView tvStatus;
    @Bind(R.id.tv_again)
    TextView tvAgain;
    @Bind(R.id.tv_memo)
    TextView tvMemo;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_entry_date)
    TextView tvEntryDate;
    @Bind(R.id.tv_office)
    TextView tvOffice;
    @Bind(R.id.tv_edu)
    TextView tvEdu;
    @Bind(R.id.tv_professional)
    TextView tvProfessional;
    @Bind(R.id.tv_job_title)
    TextView tvJobTitle;
    @Bind(R.id.lin_child_account)
    LinearLayout lin_child_account;
    @Bind(R.id.tv_shop_id)
    TextView tv_shop_id;
    @Bind(R.id.tv_shop_name)
    TextView tv_shop_name;
    @Bind(R.id.tv_main_account_id)
    TextView tv_main_account_id;



    private Customer mCustomer;

    private ClerkStatusPresenter mPresenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_clerk_status;
    }

    @Override
    public void setData() {
        super.setData();
        setListener();
        mPresenter = new ClerkStatusPresenter(this, this);
        setIntent();
        tvRright.setText("编辑");
        imgBack.setVisibility(View.VISIBLE);

    }

    private void setIntent() {
        Bundle extras = getIntent().getExtras();
        if (MyApplication.user.getType() == 0) {
            if (!extras.containsKey("customer")) {
                finish();
                return;
            }
            tvTitle.setText("店员帐号详情");
            mCustomer = (Customer) extras.getSerializable("customer");
            setCustomer(mCustomer);
        } else {
            lin_child_account.setVisibility(View.VISIBLE);
            tvRright.setVisibility(View.GONE);
            tvTitle.setText("我的资料");
            mPresenter.getemployeeinfo();
        }

    }


    public void setCustomer(Customer customer) {
        tvName.setText(customer.getName());
        tvSex.setText(customer.getGender());
        tvTel.setText(customer.getMobile());
        tvEntryDate.setText(customer.getCheckintime());
        tvOffice.setText(customer.getJob());
        tvEdu.setText(customer.getDiploma());
        tvJobTitle.setText(customer.getJobtitle());
        tvProfessional.setText(customer.getMajor());
        if (MyApplication.user.getType() == 0) {
            tvRright.setVisibility(View.VISIBLE);
        }else{
            tv_shop_id.setText(customer.getShopId());
            tv_shop_name.setText(customer.getShopName());
            tv_main_account_id.setText(customer.getMainAccount());
        }
        tvDisable.setVisibility(View.GONE);
        tvAgain.setVisibility(View.GONE);
        tvMemo.setVisibility(View.GONE);
        if (customer.getStatus() == 1) {
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_3CAF5C));
            tvStatus.setText("已启用");
            if (MyApplication.user.getType() == 0) {
                tvDisable.setVisibility(View.VISIBLE);
            }
        } else {
            if (customer.getAuditStatus() == 0) {
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_EB3030));
                tvStatus.setText("待审核");
                tvRright.setVisibility(View.GONE);
            } else if (customer.getAuditStatus() == 2) {
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_EB3030));
                tvStatus.setText("审核未通过");
                tvMemo.setVisibility(View.VISIBLE);
                tvMemo.setText(customer.getAuditMsg());
            } else {
                tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_999999));
                tvStatus.setText("已禁用");
                if (MyApplication.user.getType() == 0) {
                    tvAgain.setVisibility(View.VISIBLE);
                }

            }
        }
    }


    private void setListener() {
        imgBack.setOnClickListener(this);
        tvRright.setOnClickListener(this);
        tvDisable.setOnClickListener(this);
        tvAgain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                bundle.putSerializable("customer", mCustomer);
                showActivity(AddAndEditClerkActivity.class, bundle);
                break;
            case R.id.tv_disable:
                DialogUtils.showConfirmAndCancel(this, "确定要禁用吗？禁用后账号将不可登录", new DialogListener() {
                    @Override
                    public void onCancl(Dialog dialog) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        mPresenter.modifyuserstatus(0, mCustomer.getEmployeeid());
                    }
                }, R.string.cancel, R.string.confim);


                break;
            case R.id.tv_again:

                DialogUtils.showConfirmAndCancel(this, "确定要重新启用吗？重启之后账号将允许登录", new DialogListener() {
                    @Override
                    public void onCancl(Dialog dialog) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.dismiss();
                        mPresenter.modifyuserstatus(1, mCustomer.getEmployeeid());
                    }
                }, R.string.cancel, R.string.confim);

                break;
        }

    }

    @Override
    public void showTip(String msg) {
        Utils.showTip(mContext, msg);
    }

    @Override
    public void setSuccess(int status) {
        if(status ==0){
            showTip("账号禁用成功");
            MyApplication.getInstance().currentType =1;
        }else {
            MyApplication.getInstance().currentType =0;
            showTip("账号重新启用成功");
        }
        finish();
    }

    @Override
    public void setPresenter(ClerkStatusContract.Present presenter) {

    }
}
